package com.isesol.ime.rest.api;

import com.github.pagehelper.*;
import com.google.common.collect.*;
import com.isesol.api.rest.annoations.*;
import com.isesol.api.rest.model.base.*;
import com.isesol.arch.common.model.*;
import com.isesol.ime.Constant;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.rest.model.param.*;
import com.isesol.ime.service.*;
import com.smtcl.iplatform.machinearchive.rentservices.*;
import com.smtcl.iplatform.machinearchive.rentservices.dto.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import javax.annotation.*;
import java.util.*;

/**
 * 订阅管理
 * <p>
 * <li> 创建订阅
 * <li> 删除订阅
 * <li> 启用订阅
 * <li> 停用订阅
 * <li> 订阅下发
 * <li> 订阅全局下发
 * <li> 机床已选报文
 * <li> 机床可选报文
 * </p>
 *
 * @author peter zhang
 * @author Wang Jun
 */
@Service
@RestServiceClass(url = "subscription")
public class SubscriptionResource extends BusinessResources{

	@Resource
	SubscriptionService subscriptionService;

	@Resource
	MessageService messageService;

	@Resource
	ISubMachineService subMachineService;

	/**
	 * 新增订阅
	 * 一次订阅操作，只涉及一种类型，频率或事件,需要传入ruleName=time 或event
	 *
	 * @param request

	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "create", requestType = MachineToolSubscriptionRequest.class)

	public void create(MachineToolSubscriptionRequest request) throws Exception {


		LOGGER.info(com.isesol.ime.Constant.MODEL_NAME_SUBSCRIPTION + "create接口调用,request={}", request);

		//没传 machineType 则默认"i5"
		String machineType = request.getMachineType();
		if (StringUtils.isEmpty(machineType)) {

			request.setMachineType(com.isesol.ime.Constant.DEFAULT_MACHINE_TYPE);
		}

		//没传 ruleName 则默认time
		String ruleName = request.getRuleName();

		if (StringUtils.isEmpty(ruleName)){

			request.setRuleName(com.isesol.ime.Constant.DEFAULT_RULE_NAME);
		}

		SubscriptionDTO subscriptionDTO = modelMapper.map(request, SubscriptionDTO.class);

		subscriptionService.create(subscriptionDTO);

	}

	/**
	 * push
	 *
	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "push")
	public void push(@RestParam(name = "machineId") String machineId,
	                 @RestParam(name = "subId") String subId) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "push接口调用,machineId={},subId={}", machineId, subId);

		subscriptionService.sendDownloadOrder(machineId, subId);

	}

	/**
	 * push all
	 *
	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "pushAll")
	public void push(@RestParam(name = "machineType", required = false) String machineType) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "pushAll接口调用,machineType={}", machineType);

		subscriptionService.pushAll(machineType);

	}

	/**
	 * 启用订阅
	 * @param machineId
	 * @param machineType
	 */
	@Transactional
	@RestServiceMethod(url = "initSubscription")
	public void initSubscription(@RestParam(name = "machineId") String machineId, @RestParam(name = "machineType") String machineType){

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "initSubscription接口调用,machineType={}", machineType);

		subscriptionService.initSubscription(machineId, machineType);

	}

	/**
	 * 启用订阅
	 *
	 * @param request
	 *
	 * @return

	 * @throws Exception
	 */
	@Transactional(isolation = Isolation.READ_COMMITTED)
	@RestServiceMethod(url = "startUsing", requestType = MachineToolSubscriptionRequest.class)
	public void startUsing(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "startUsing接口调用,request={}", request);

		Result result = subscriptionService.startUsing(request.getId());

		parseResult(result);
	}

	/**
	 * 停用订阅
	 *
	 * @param request
	 *
	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "stopUsing", requestType = MachineToolSubscriptionRequest.class)
	public void stopUsing(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "stopUsing接口调用,request={}", request);

		subscriptionService.stopUsing(request.getId());

	}

	/**
	 * 删除订阅
	 *
	 * @param request

	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "del", requestType = MachineToolSubscriptionRequest.class)
	public void del(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "del接口调用,request={}", request);

		subscriptionService.del(request.getId());

	}

	/**
	 * 该机床已选报文
	 *
	 * @param request

	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "getSelected", requestType = MachineToolSubscriptionRequest.class)
	public List<com.isesol.ime.dal.model.dto.MessageDTO> getByMachineId(MachineToolSubscriptionRequest request) throws Exception {


		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "getSelected接口调用,request={}", request);

		//return subscriptionService.getSelectedMessages(request.getMachineId());

		//todo fix this
		if (StringUtils.isBlank(request.getRuleName())) {

			request.setRuleName("time");
		}
		List<com.isesol.ime.dal.model.dto.MessageDTO> list = subscriptionService.getSelectedMessages(request.getRuleName(), request.getMachineId());

		return list;

	}

	/**
	 * 该机床可选报文
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RestServiceMethod(url = "getSelectable", requestType = MachineToolSubscriptionRequest.class)
	public List<com.isesol.ime.dal.model.dto.MessageDTO> getMsgssByMachineId(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "getMsgssByMachineId接口调用,request={}", request);

		//如果没有传 machineType 则默认"i5"
		String machineType = request.getMachineType();
		if (StringUtils.isEmpty(machineType)) {
			machineType = "i5";
		} else {

			machineType = request.getMachineType();
		}

		List<com.isesol.ime.dal.model.dto.MessageDTO> list = subscriptionService.getSelectableMessages(
				machineType, request.getMachineId(), request.getRuleName());

		return list;

	}

	/**
	 * 查询 分页
	 */
	@Transactional
	@RestServiceMethod(url = "find", requestType = MachineToolSubscriptionRequest.class)
	public PageInfo find(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "find接口调用,request={}", request);

		SubscriptionDTO subscriptionDTO = modelMapper.map(request, SubscriptionDTO.class);

		return subscriptionService.find(subscriptionDTO,
				request.getRestRequest().getPage(),
				request.getRestRequest().getRows());

	}

	/**
	 * 获得机床列表
	 *
	 * @return
	 */
	@Transactional
	@RestServiceMethod(url = "getAllMachine")
	public List<MachineInfoDTO> getAllMachine() {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "getAllMachine");

		List<MachineInfoDTO> machineInfoDTOList = subMachineService.getAllMachine();

		for (MachineInfoDTO machineInfoDTO : machineInfoDTOList) {

			machineInfoDTO.setTypeId(machineInfoDTO.getTypeName());
		}
		return machineInfoDTOList;

	}

	/**
	 * 根据机床类型获得机床列表
	 *
	 * @param typeId
	 * @return
	 */
	@Transactional
	@RestServiceMethod(url = "getMachineByType")

	public List<MachineInfoDTO> getMachieByType(@RestParam(name = "typeId") String typeId) {


		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "getMachieByType,typeId ={} ", typeId);

		List<MachineInfoDTO> allMachineType = subMachineService.getAllMachineType();

		for (MachineInfoDTO machineInfoDTO : allMachineType) {
			if (typeId.equals(machineInfoDTO.getTypeName())) {

				return subMachineService.getMachineInfoByTypeId(machineInfoDTO.getTypeId());
			}
		}
		return Lists.newArrayList();
	}

	@Transactional
	@RestServiceMethod(url = "batchCopy", requestType = MachineToolBatchCopyRequest.class)
	public void batchCopy(MachineToolBatchCopyRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "batchCopy,request ={} ", request);

		subscriptionService.batchCopy(request.getSourceMachineId(), request.getMachineIds());

	}

	@Transactional
	@RestServiceMethod(url = "initSubscription", requestType = MachineToolSubscriptionRequest.class)
	public void initSubscription(MachineToolSubscriptionRequest request) throws Exception {

		LOGGER.info(Constant.MODEL_NAME_SUBSCRIPTION + "initSubscription,request ={} ", request);

		SubscriptionDTO subscriptionDTO = modelMapper.map(request, SubscriptionDTO.class);

		Result result = subscriptionService.initSubscription(subscriptionDTO.getMachineId(), subscriptionDTO.getMachineType());

		parseResult(result);

	}

}
